﻿using System;
using System.Collections.Generic;
using System.Web;

using Net.DataAccess;

namespace Net.BusinessLogic
{
	public class Article : BizObject
	{
		private long _id = 0;
		public long ID { get { return _id; } set { _id = value; } }

		private DateTime _addedDate = DateTime.Now;
		public DateTime AddedDate { get { return _addedDate; } set { _addedDate = value; } }

		private string _addedBy = "";
		public string AddedBy { get { return _addedBy; } set { _addedBy = value; } }

		private string _title = "";
		public string Title { get { return _title; } set { _title = value; } }

		private string _summary = "";
		public string Summary { get { return _summary; } set { _summary = value; } }

		private string _body = "";
		public string Body { get { return _body; } set { _body = value; } }

		private DateTime _timePoint = DateTime.Now;
		public DateTime TimePoint { get { return _timePoint; } set { _timePoint = value; } }

		public Article(
			long id,
			DateTime addedDate, string addedBy, string title,
			string summary, string body, DateTime timePoint)
		{
			this.ID = id;

			this.AddedDate = AddedDate;
			this.AddedBy = addedBy;
			this.Title = title;
			this.Summary = summary;
			this.Body = body;
			this.TimePoint = timePoint;
		}

		public static Article FindByID(int id)
		{
			Article obj = null;
			string key = "article_"+ id.ToString();
			if (Globals.Settings.EnableCaching && Cache[key] != null)
				obj = (Article)Cache[key];
			else
			{
				obj = Get(Provider.Article.FindByID(id));
				CacheData(key, obj);
			}
			return obj;
		}

		public static List<Article> FindByTitle(string title)
		{
			List<Article> obj = new List<Article>();
			string key = "articles_"+ title.ToLower();
			if (Settings.EnableCaching && Cache[key] != null)
				obj = (List<Article>)Cache[key];
			else
			{
				obj = GetList(Provider.Article.FindByTitle(title));
				CacheData(key, obj);
			}
			return obj;
		}

		public static int Insert(
			int categoryID,
			string title,
			string summary,
			string body,
			DateTime timePoint)
		{
			return 0;
		}

		/*******************
		 * PRIVATE METHODS *
		 *******************/
		private static Article Get(ArticleDetails r)
		{
			if (r == null) return null;
			return new Article(r.ID, r.AddedDate, r.AddedBy, r.Title, r.Summary, r.Body, r.TimePoint);
		}
		private static List<Article> GetList(List<ArticleDetails> rs)
		{
			List<Article> items = new List<Article>();
			foreach (ArticleDetails r in rs)
				items.Add(Get(r));
			return items;
		}
	}
}